package com.danny.service;

import com.spring.BeanPostProcessor;
import com.spring.BeansException;
import com.spring.Commonent;
import com.spring.DyValue;

import java.lang.reflect.Field;

@Commonent
public class DyValueBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        try {
            for (Field field : bean.getClass().getDeclaredFields()) {
                if (field.isAnnotationPresent(DyValue.class)) {
                    field.setAccessible(true);
                    field.set(bean, field.getAnnotation(DyValue.class).value());
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return bean;
    }
}
